package com.jsbb.util;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

/**
 * JWT工具类
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
public class JwtUtils {

    private static String secret = "jsbb20210101";

    public static String build(String content) {
        String jwt = Jwts.builder()
                .claim("content", content)
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
        return jwt;
    }

    public static String parse(String token) {
        if (token != null) {
            try {
                Claims body = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
                return JSON.parseObject(JSON.toJSONString(body)).getString("content");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
